

<?php


        //获取地址栏中参数名为ID 的值
        $id = !empty($_GET['id'])?$_GET['id']:'';

        if(!empty($_POST)){

             $name = $_POST['name'];

             $pid = $_POST['pid'];

            // 编写插入的sql语句
            $sql = "update `cate` set `name`='$name',`pid`='$pid' where `id`=$id";

            mysqli_query($link,$sql);

            $_rows = mysqli_affected_rows($link);

            if($_rows>=0){

               echo '<script>alert("分类编辑成功");location.href="index.php?c=cate&&a=list&&admin=1"; </script>';

            }else{

        echo '<script>alert("分类编辑失败");history.go(-1) </script>';
      }
        }


        //查询当前要编辑的数据
        $sql =  "select * from `cate` where `id`=$id";

        $result = mysqli_query($GLOBALS['link'],$sql);

        $date = mysqli_fetch_assoc($result);
        $cate=array();

        $cate = editCate($date);

        function editCate($date,$cate=array()){
                $pid=$date['pid'];
            $sql =  "select * from `cate` where `pid`=$pid";

            $result = mysqli_query($GLOBALS['link'],$sql);

            //循环将查询到的数据存在同一数组中
            while($arr = mysqli_fetch_assoc($result)){

                //将查询到的数据先存储到临时数组中
                $cate[$date['id']][]=$arr;
                }
            if($pid!=0){

                    //找父级
                    $sql =  "select * from `cate` where `id`=$pid";

                    $resultt = mysqli_query($GLOBALS['link'],$sql);

                    $date = mysqli_fetch_assoc($resultt);

                    $cate = editCate($date,$cate);
                }
                return $cate;
        }

        $cate = array_reverse($cate,true);
// print_r($cate);die;



 //载入后台公共视图
   include_once (VIEW_PATH.'admin/layout.html');

?>